//== 找回密码
let mRetrievePassword = function () {
    // 当前激活向导页
    let currentWizardSelector = '.e-wizard-step[data-step-status="current"]';

    let wizard;
    let initWizard = function () {
        //== 初始化
        wizard = new EWizard('#wizard', {
            startStep: 1
        });

        //== 下一步
        wizard.on('beforeNext', function (wizardObj) {
            let needValid = $(currentWizardSelector).find('input,select');
            if (needValid.length > 0) {
                if (!needValid.valid()) {
                    // 表单验证失败 不跳转到下一步
                    wizardObj.stop();
                }
            }
        });
    };
    /**
     * 发送验证邮件
     */
    let sendMail = function () {
        let needValid = $(currentWizardSelector).find('input');
        if (needValid.length > 0) {
            if (needValid.valid()) {
                EUtil.ajax({
                    url: ETool.getBaseUrl() + 'send/mail',
                    wait: '.card',
                    data: {
                        username: $('#username').val(),
                        mail: $('#email').val()
                    },
                    success: function (res) {
                        wizard.goToStep(3);
                    }
                });
            }
        }
    };
    /**
     * 验证验证码是否正确
     */
    let verifiesCode = function () {
        let username = $('#username').val();
        let code = $('#verificationCode').val();
        let needValid = $(currentWizardSelector).find('input');
        if (needValid.length > 0) {
            if (needValid.valid()) {
                EUtil.ajax({
                    url: ETool.getBaseUrl() + 'verifies/' + username + '/' + $.md5(code),
                    wait: '.card',
                    success: function (res) {
                        wizard.goToStep(4);
                    }
                });
            }
        }
    };
    /**
     * 切换显示/隐藏密码
     */
    let showPassword = function (el) {
        let $el = $(el);
        let $password = $('#password');
        if('password' === $password.attr('type')){
            $password.attr('type', 'text');
            $el.find('i').removeClass().addClass('la la-eye');
        } else {
            $password.attr('type', 'password');
            $el.find('i').removeClass().addClass('la la-eye-slash');
        }
    };
    /**
     * 重置密码
     */
    let resetPassword = function () {
        let needValid = $(currentWizardSelector).find('input');
        if (needValid.length > 0) {
            if (needValid.valid()) {
                let username = $('#username').val();
                let code = $('#verificationCode').val();
                EUtil.ajax({
                    url: ETool.getBaseUrl() + 'reset/password/' + username + '/' + $.md5(code),
                    wait: '.card',
                    data:{
                        password: $.md5($('#password').val())
                    },
                    success: function (res) {
                        wizard.goToStep(5);
                    }
                });
            }
        }
    };
    /**
     * 绑定重置密码
     */
    let bindResetPassword = function () {
        $('#reset-password').click(function () {
            resetPassword();
        });    
    };
    /**
     * 绑定切换显示/隐藏密码事件
     */
    let bindShowPassword = function () {
        $('#show-password').click(function () {
            showPassword(this);
        });
    };
    /**
     * 绑定点击验证验证码事件
     */
    let bindVerificationCode = function () {
        $('#verification-code').click(function () {
            verifiesCode();
        });    
    };
    /**
     * 绑定发送mail事件
     */
    let bindSendMailClick = function () {
        $('#send-mail').click(function () {
            sendMail();
        });
    };
    /**
     * 绑定点击找回方式事件
     */
    let bindChoseModel = function () {
        $('.retrieve-model .model').click(function () {
            let model = $(this).data('type');
            if ('mail' === model) {
                // 通过邮箱找回
                wizard.goToStep(2);
            } else if ('phone' === model) {
                // 通过手机找回
                ETool.warnTip(commonTips.fail, '暂未开放，请使用邮箱找回密码');
            }
        });
    };
    /**
     * 绑定事件
     */
    let bind = function () {
        bindChoseModel();
        bindSendMailClick();
        bindVerificationCode();
        bindShowPassword();
    };
    return {
        //== 初始化页面
        init: function () {
            ETool.setBaseUrl(basePath + '/sys/user/retrieve/password/');
            initWizard();
            bindResetPassword();
            bind();
        }
    };
}();
//== 初始化
$(document).ready(function () {
    mRetrievePassword.init();
});